package com.caregiver.watch.business.user.model.vo;

import com.caregiver.watch.business.user.enums.CareRecipientWearingHabitEnum;
import com.caregiver.watch.business.user.enums.CareRelationshipEnum;
import com.caregiver.watch.business.user.enums.DeviceOnlineStatusEnum;
import com.caregiver.watch.business.user.model.po.UserCareRecipient;
import com.caregiver.watch.business.user.model.po.UserDevice;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Description: 亲友设备
 * @Author 疆戟
 * @Date 2025/8/24 21:14
 * @Version 1.0
 */
@Data
public class ContactDeviceDetailVo {

    /**
     * 设备ID
     */
    @Schema(description = "设备ID")
    private Long deviceId;

    /**
     * 设备名称
     */
    @Schema(description = "设备名称")
    private String deviceName;

    /**
     * 设备UUId
     */
    @Schema(description = "设备UUId")
    private String deviceUuid;

    /**
     * 设备型号
     */
    @Schema(description = "设备型号")
    private String deviceModel;

    /**
     * 系统版本
     */
    @Schema(description = "系统版本")
    private String systemVersion;

    /**
     * 电量
     */
    @Schema(description = "电量")
    private Integer battery;

    /**
     * 跟新时间
     */
    @Schema(description = "跟新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateTime;

    /**
     * 在线状态
     */
    @Schema(description = "在线状态(在线,离线)")
    private DeviceOnlineStatusEnum status;

    /**
     * 被照护人ID
     */
    @Schema(description = "被照护人ID")
    private Long recipientId;
    /**
     * 被照护人姓名
     */
    @Schema(description = "被照护人姓名")
    private String recipientName;

    /**
     * 与用户关系（本人、父亲、母亲、配偶等）
     */
    @Schema(description = "与用户关系（本人、父亲、母亲、配偶等）")
    private CareRelationshipEnum relationship;

    /**
     * 性别（0-未知 1-男 2-女）
     */
    @Schema(description = "性别（0-未知 1-男 2-女）")
    private Integer gender;

    /**
     * 生日
     */
    @Schema(description = "生日")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private LocalDate birthday;

    // 腕围
    @Schema(description = "腕围(mm)")
    private Integer wristCircumference;

    // 身高
    @Schema(description = "身高(cm)")
    private Integer height;

    // 体重
    @Schema(description = "体重(kg)")
    private Integer weight;

    /**
     * 佩戴习惯
     */
    @Schema(description = "佩戴习惯 UNKNOWN 未知 LEFT_HAND 左手 RIGHT_HAND 右手")
    private CareRecipientWearingHabitEnum wearingHabit;

    public static ContactDeviceDetailVo build(UserDevice userDevice) {
        ContactDeviceDetailVo contactDeviceDetailVo = new ContactDeviceDetailVo();
        contactDeviceDetailVo.deviceId = userDevice.getDeviceId();
        contactDeviceDetailVo.deviceName = userDevice.getDeviceName();
        contactDeviceDetailVo.deviceUuid = userDevice.getDeviceUuid();
        contactDeviceDetailVo.deviceModel = userDevice.getDeviceModel();
        contactDeviceDetailVo.systemVersion = userDevice.getSystemVersion();
        contactDeviceDetailVo.battery = userDevice.getBatteryLevel();
        contactDeviceDetailVo.updateTime = userDevice.getUpdateTime();
        contactDeviceDetailVo.status = DeviceOnlineStatusEnum.parse(userDevice.getOnlineStatus());
        return contactDeviceDetailVo;
    }

    public void setUserCareRecipient(UserCareRecipient userCareRecipient) {
        if (Objects.isNull(userCareRecipient)) {
            return;
        }
        this.recipientId = userCareRecipient.getRecipientId();
        this.recipientName = userCareRecipient.getName();
        this.relationship = CareRelationshipEnum.parse(userCareRecipient.getRelationship());
        this.gender = userCareRecipient.getGender();
        this.birthday = userCareRecipient.getBirthday();
        this.wristCircumference = userCareRecipient.getWristCircumference();
        this.height = userCareRecipient.getHeight();
        this.weight = userCareRecipient.getWeight();
        this.wearingHabit = CareRecipientWearingHabitEnum.parse(userCareRecipient.getWearingHabit());
    }
}
